using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace BookBarter
{
    public class DatabaseConnectionManager
    {
        private static String _cnxString = "";

        private static DatabaseConnectionManager _current;
        /// <summary>
        /// Property to get current database connection
        /// </summary>
        public static DatabaseConnectionManager Current
        {
            get
            {
                if (DatabaseConnectionManager._current == null)
                {
                    DatabaseConnectionManager._current = new DatabaseConnectionManager();
                }
                return DatabaseConnectionManager._current;
            }
            set { DatabaseConnectionManager._current = value; }
        }


        private SqlConnection _connection;

        /// <summary>
        ///  public property to get the connection
        /// </summary>
        public SqlConnection Connection
        {
            get
            {
                _connection = new SqlConnection(_cnxString);
                _connection.Open();
                return _connection;
            }
            private set { _connection = value; }
        }

        /// <summary>
        /// Initialize the singleton
        /// </summary>
        private DatabaseConnectionManager()
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
            builder.DataSource = Properties.Settings.Default.Server;
            builder.InitialCatalog = Properties.Settings.Default.Database;
            builder.Password = Properties.Settings.Default.Password;
            builder.UserID = Properties.Settings.Default.User;
            _cnxString = builder.ToString();
        }
    }
}
